<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 18/12/3
 * Time: 下午7:10
 */

namespace app\index\controller;

use think\Controller;
class User extends Controller
{
    public function add(){
        $data = $this->request->param();
//        print_r($data);
        //$data传过来是一个数组
        /*比如
         * Array
              (
                [email] => 222@qq.com
                [username] => wsdfs
                [password] => 123123
                [password2] => 123123
                )
         */
        //前端验证是不可信的，需要服务端再次验证
        //定义验证规则
        $rules = [
            'email'     => 'require|email',
            'username'  => 'require|min:2',
            'password'  => 'require|length:6,12',
            'password2' => 'require|confirm:password'
        ];
        //定义验证失败后的错误信息
        $msg = [
            'email.require' => '邮箱为必填项',
            'email.email'   => '邮箱格式有误',
            'username.require' => '昵称为必填项',
            'username.min' => '昵称最小需要2个字符',
            'password.require' => '密码为必填项',
            'password.length'  => '密码长度应在6-12位之间',
            'password2.require' => '重复密码为必填项',
            'password2.confirm' => '两次输入密码不一致'
        ];
        //执行验证
        $res = $this->validate($data,$rules,$msg);
        // print_r($res);

        //验证失败返回数据
        if ($res !== true ){
            return ['code'=>0,'info'=> $res];
        }


        //检验邮箱是否已经被注册
        $m = new \app\index\model\User();

        if ($m->get(['email'=>$data['email']])){
            return ['code'=>0,'info'=>'该邮箱已注册'];
        }

        //密码加密处理，哈希加密法
        $data['password'] = password_hash($data['password'],PASSWORD_DEFAULT);
        //使用模型进行数据的插入
        return $m->save($data);

















    }

}